﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace EBA.Framework
{

    /// <summary>
    /// 插件列表配置信息
    /// </summary>
    [ConfigurationCollection(typeof(BundleConfigurationElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class BundleConfigurationElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 新建一个插件配置节点
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new BundleConfigurationElement();
        }

        /// <summary>
        /// 获取插件配置节点标识
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BundleConfigurationElement)element).Id;
        }

        /// <summary>
        /// 新增一个插件配置节点
        /// </summary>
        /// <param name="bundle"></param>
        public void Add(BundleConfigurationElement bundle)
        {
            BaseAdd(bundle);
        }

        /// <summary>
        /// 迭代获取插件配置节点
        /// </summary>
        public IEnumerable<BundleConfigurationElement> AsEnumerable
        {
            get
            {
                for (var i = 0; i < this.Count; i++)
                {
                    yield return (BundleConfigurationElement)BaseGet(i);
                }
            }
        }

        /// <summary>
        /// 根据插件标识，获取对应的插件配置节点。
        /// </summary>
        /// <param name="bundleId">插件标识</param>
        /// <returns></returns>
        public BundleConfigurationElement GetElement(string bundleId)
        {
            foreach (var item in this.AsEnumerable)
            {
                if (item.Id == bundleId)
                {
                    return item;
                }
            }

            return null;

        }


      
    }
}
